package hjkj.service;


import hjkj.bean.ConfigInit;
import hjkj.bean.Header;
import hjkj.bean.MsgJ01;
import hjkj.data.CMDCode;
import hjkj.util.DataManage;

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.Arrays;

/**
 * Created by YUB on 17/3/13.
 */
public class MsgJ01Service extends BaseService implements Runnable{

    private MsgJ01 msgJ01;
    private byte[] j01Array;

    public MsgJ01Service(MsgJ01 msgJ01) throws UnknownHostException {
        super();
        this.msgJ01 = msgJ01;
    }

    @Override
    public void run() {
        if(createSocket()){
            //头部封装
            header = new Header();
            header = header.setJHeader(CMDCode.MSG_J, msgJ01.getDataLen(),
                    header.getType("X", "J"), (byte)0);

            //j01消息封装
            j01Array = msgJ01.getBytes();

            //拼接头部，j01消息，crc校验码
            sendData = getSendData(header, j01Array);
            System.out.println("发送数据sendData：" + Arrays.toString(sendData) + "\n数据长度：" + sendData.length
                    + "\nj01Array：" + Arrays.toString(j01Array));

            //发送
            sendPacket = new DatagramPacket(sendData, sendData.length, address, port);
            try {
                socket.send(sendPacket);
            } catch (IOException e) {
                System.out.println("J01消息界面直接发送失败，请检查socket");
                e.printStackTrace();
            }
            System.out.println("发送的地址是：" + address + ",端口号是：" + port);
        }
    }

}
